<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
// load base class if needed
require_once( APPPATH . 'controllers/base/OperatorBase.php' );

class silabus extends ApplicationBase {

    // constructor
    public function __construct() {
        parent::__construct();
        // load model
        $this->load->model('m_tahunajar');
        $this->load->model('m_silabus');
        $this->load->model('m_dosen');
        //--
        $this->smarty->assign('silabus', $this->m_silabus);
        // load library
        $this->load->library('tnotification');
        $this->load->library('pagination');
    }

    // halaman list data
    public function index() {
        // set page rules
        $this->_set_page_rule("R");
        // set template content
        $this->smarty->assign("template_content", "monitoring/silabus/index.html");
        // tahun
        $rs_tahun = $this->m_silabus->get_tahun_ajaran();
        $rs_tahun = !empty($rs_tahun) ? $rs_tahun : array("0" => array("tahun" => date('Y')));
        $this->smarty->assign('rs_tahun', $rs_tahun);
        // semester
        $sms = array(1, 2, 3, 4, 5, 6, 7, 8);
        $this->smarty->assign('rs_sms', $sms);
        // get ta
        $rs_ta = $this->m_tahunajar->get_detail_data_tahunajar();
        // session
        $search = $this->tsession->userdata('search_silabus');
        $search['semester'] = !empty($search['semester']) ? $search['semester'] : $rs_ta['semester'];
        $search['tahun'] = !empty($search['tahun']) ? $search['tahun'] : $rs_ta['tahun'];
        $this->smarty->assign('search', $search);
        // params
        $tahun = $search['tahun'];
        $semester = $search['semester'];
        $nama_lengkap = !empty($search['nama_lengkap']) ? "%" . $search['nama_lengkap'] . "%" : "%";
        //--
        $params = array($tahun, $semester, $nama_lengkap);
        /* start of pagination --------------------- */
        // pagination
        $config['base_url'] = site_url("monitoring/silabus/index/");
        $config['total_rows'] = $this->m_silabus->get_total_dosen($params);
        $config['uri_segment'] = 4;
        $config['per_page'] = 20;
        $this->pagination->initialize($config);
        $pagination['data'] = $this->pagination->create_links();
        // pagination attribute
        $start = $this->uri->segment(4, 0) + 1;
        $end = $this->uri->segment(4, 0) + $config['per_page'];
        $end = (($end > $config['total_rows']) ? $config['total_rows'] : $end);
        $pagination['start'] = ($config['total_rows'] == 0) ? 0 : $start;
        $pagination['end'] = $end;
        $pagination['total'] = $config['total_rows'];
        // pagination assign value
        $this->smarty->assign("pagination", $pagination);
        $this->smarty->assign("no", $start);
        /* end of pagination ---------------------- */
        // get data
        $params = array($tahun, $semester, $nama_lengkap, ($start - 1), $config['per_page']);
        $result = $this->m_silabus->get_data_dosen($params);
        $this->smarty->assign('rs_id', $result);
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    // proses search
    public function search_process() {
        // set page rules
        $this->_set_page_rule("R");
        //--
        if ($this->input->post('save') == 'Cari') {
            // params
            $params = array(
                "tahun" => $this->input->post('tahun'),
                "semester" => $this->input->post('semester'),
                "nama_lengkap" => $this->input->post('nama_lengkap')
            );
            // set session
            $this->tsession->set_userdata('search_silabus', $params);
        } else {
            // unset session
            $this->tsession->unset_userdata('search_silabus');
        }
        // redirect
        redirect('monitoring/silabus');
    }

    // halaman detail 
    public function detail($tahun = "", $semester = "", $dosen_kd = "") {
        // set page rules
        $this->_set_page_rule("R");
        // set template content
        $this->smarty->assign("template_content", "monitoring/silabus/detail.html");
        // tahun
        $this->smarty->assign('tahun', $tahun);
        // semester
        $this->smarty->assign('semester', $semester);
        // dosen
        $this->smarty->assign('dosen_kd', $dosen_kd);
        // data dosen
        $rs_dosen = $this->m_dosen->get_detail_data_dosen($dosen_kd);
        $this->smarty->assign('rs_dosen', $rs_dosen);
        // data
        $rs_id = $this->m_silabus->get_data_makul_dosen(array($tahun, $semester, $dosen_kd));
        $this->smarty->assign('rs_id', $rs_id);
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    // cetak silabus format pdf
    public function download_files($silabus_kd = "") {
        //set rule
        $this->_set_page_rule("R");
        // load library
        $this->load->library('tcpdf');
        // info
        $rs_info = $this->m_silabus->get_detail_info_silabus($silabus_kd);
        // data
        $rs_id = $this->m_silabus->get_data_item_silabus($silabus_kd);
        /*
         *  print out
         */
        // page setting
        $this->tcpdf->SetPrintHeader(false);
        $this->tcpdf->SetPrintFooter(false);
        $this->tcpdf->SetDisplayMode('real');
        $this->tcpdf->SetMargins(10, 10, 10, false);
        $this->tcpdf->SetFont('helvetica', '', 10);
        $this->tcpdf->SetAutoPageBreak(TRUE, 0);
        $this->tcpdf->AddPage('L', 'F4');
        // html content
        $html = '<table cellpadding="2">';
        $html .= '  <tr>';
        $html .= '      <td width="15%"><b>MATA KULIAH</b></td>';
        $html .= '      <td width="3%" align="center"><b>:</b></td>';
        $html .= '      <td width="82%"><b>' . $rs_info['makul_nm'] . '</b></td>';
        $html .= '  </tr>';
        $html .= '  <tr>';
        $html .= '      <td><b>SKS</b></td>';
        $html .= '      <td align="center"><b>:</b></td>';
        $html .= '      <td><b>' . $rs_info['makul_sks'] . '</b></td>';
        $html .= '  </tr>';
        $html .= '  <tr>';
        $html .= '      <td><b>JURUSAN</b></td>';
        $html .= '      <td align="center"><b>:</b></td>';
        $html .= '      <td><b>' . $rs_info['jurusan_nm'] . '</b></td>';
        $html .= '  </tr>';
        $html .= '  <tr>';
        $html .= '      <td><b>DESKRIPSI</b></td>';
        $html .= '      <td align="center"><b>:</b></td>';
        $html .= '      <td><b>' . $rs_info['silabus_deskripsi'] . '</b></td>';
        $html .= '  </tr>';
        $html .= '  <tr>';
        $html .= '      <td></td>';
        $html .= '      <td align="center"></td>';
        $html .= '      <td></td>';
        $html .= '  </tr>';
        $html .= '</table>';
        $html .= '<table cellpadding="2" border="0.5" style="font-size: 8;">';
        $html .= '  <tr>';
        $html .= '      <td width="5%" align="center"><b>MINGGU</b></td>';
        $html .= '      <td width="20%" align="center"><b>KOMPETENSI DASAR</b></td>';
        $html .= '      <td width="20%" align="center"><b>MATERI POKOK</b></td>';
        $html .= '      <td width="25%" align="center"><b>INDIKATOR KOMPETENSI</b></td>';
        $html .= '      <td width="10%" align="center"><b>SUMBER</b></td>';
        $html .= '      <td width="10%" align="center"><b>PENILAIAN</b></td>';
        $html .= '      <td width="10%" align="center"><b>KETERANGAN</b></td>';
        $html .= '  </tr>';
        // DATA
        foreach ($rs_id as $data) {
            $html .= '<tr>';
            $html .= '  <td align="center">' . $data['minggu'] . '</td>';
            $html .= '  <td>- ' . $data['kompetensi_dasar'] . '</td>';
            // materi pokok
            $rs_materi = $this->m_silabus->get_data_materi($data['silabus_detail_kd']);
            $html .= '  <td>';
            foreach ($rs_materi as $materi) {
                $html .= '- ' . $materi['materi_content'] . '<br />';
            }
            $html .= '  </td>';
            // indikator kompetensi
            $rs_indikator = $this->m_silabus->get_data_indikator($data['silabus_detail_kd']);
            $html .= '  <td>';
            foreach ($rs_indikator as $indikator) {
                $html .= '' . $indikator['indikator_content'] . '<br />';
            }
            $html .= '  </td>';
            // sumber
            $rs_sumber = $this->m_silabus->get_data_sumber($data['silabus_detail_kd']);
            $html .= '  <td>';
            foreach ($rs_sumber as $sumber) {
                $html .= '- ' . $sumber['sumber_content'] . '<br />';
            }
            $html .= '  </td>';
            $html .= '  <td>- ' . $data['evaluasi'] . '</td>';
            $html .= '  <td>- ' . $data['keterangan'] . '</td>';
            $html .= '</tr>';
        }
        $html .= '</table>';
        // filename
        $filename = str_replace(" ", "_", $rs_info['silabus_deskripsi']);
        // write
        $this->tcpdf->writeHTML($html, true, false, true, false, '');
        // download output
        $this->tcpdf->Output('' . strtoupper($filename) . '.pdf', 'D');
    }

}